﻿// ===============================================================================
// XCI.Parent 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using System;
using XCI.Sys.Model;
using XCI.Sys.Service;
using XCI.Windows;
using XCI.Helper;

namespace XCI.Sys.Forms
{
    public partial class FrmPermissionEdit : BaseForm
    {
        public FrmPermissionEdit(EditFormArgs args)
        {
            InitializeComponent();
            layoutControl.Args = args;
        }

        private async void Form_Load(object sender, EventArgs e)
        {
            await layoutControl.BindingModelAsync();
        }

        private async void BtnSave_Click(object sender, EventArgs e)
        {
            await layoutControl.SaveModelAsync();
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            DialogOk();
        }

        private void layoutControl_BindingForm(object sender, EditFormPanelBindingEventArgs e)
        {
            e.Task = async () =>
            {
                var model = await SysModuleService.Instance.GetOrCreateAsync(e.Args,false);
                await parentModuleTreeList.LoadData(e.Args.Created, model);
                bindingSource.DataSource = model;
                WinFormHelper.SetTreeRootValue(parentModuleTreeList, e.Args.ParentId);
            };
        }

        private void layoutControl_SaveForm(object sender, EditFormPanelSaveEventArgs e)
        {
            e.Task = async () => await SysModuleService.Instance.SaveAsync(e.Args.Created, (SysModule)e.Model);
        }

        private async void layoutControl_ResetForm(object sender, EditFormPanelResetEventArgs e)
        {
            bindingSource.DataSource = await SysModuleService.Instance.NewPermissionAsync(layoutControl.Args.ParentId);
            await layoutControl.BindingModelAsync();
        }
    }
}